<?php

namespace plugin\ycMassage\app\model;

use plugin\ycMassage\utils\enum\CouponCodeState;
use plugin\ycMassage\utils\enum\CouponValidType;
use think\facade\Db;

class MassageCouponCode extends Model
{
    public static function receive($MassageCoupon, $MassageCouponCode, $uid)
    {
        Db::startTrans();
        try {
            $nowDate = date('Y-m-d H:i:s');
            switch ($MassageCoupon->valid_type) {
                case CouponValidType::TIME['value']:
                    list($start_time, $end_time) = $MassageCoupon->valid_time;
                    if ($start_time && $end_time) {
                        if ($end_time < $nowDate) {
                            throw new \Exception('优惠券已过期');
                        }
                        $MassageCouponCode->start_time = $start_time;
                        $MassageCouponCode->end_time = $end_time;
                    } else if ($start_time) {
                        $MassageCouponCode->start_time = $start_time;
                    } else if ($end_time) {
                        $MassageCouponCode->end_time = $end_time;
                    }
                    break;
                case CouponValidType::DAY['value']:
                    $MassageCouponCode->start_time = $nowDate;
                    $MassageCouponCode->end_time = date('Y-m-d H:i:s', time() + $MassageCoupon->valid_day * 24 * 3600);
                    break;
            }
            $MassageCouponCode->receive_time = $nowDate;
            $MassageCouponCode->uid = $uid;
            $MassageCouponCode->save();
            $MassageCoupon->num = Db::raw('num-1');
            $MassageCoupon->receive_num = Db::raw('receive_num+1');
            $MassageCoupon->save();
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            throw $th;
        }
    }
    /**
     * 创建并领取优惠券
     * @param MassageCoupon $MassageCoupon
     * @param MassageUser $UserModel
     */
    public static function createAndReceive($MassageCoupon, $UserModel)
    {
        $MassageCoupon->sum = Db::raw('sum+1');
        $MassageCoupon->receive_num = Db::raw('receive_num+1');
        $MassageCoupon->save();

        $MassageCouponCode = new MassageCouponCode;
        $MassageCouponCode->saas_appid = $UserModel->saas_appid;
        $MassageCouponCode->uid = $UserModel->id;
        $MassageCouponCode->coupon_id = $MassageCoupon->id;
        $MassageCouponCode->state = CouponCodeState::YES['value'];
        $MassageCouponCode->code = self::getCouponCode($MassageCoupon->id);

        $nowDate = date('Y-m-d H:i:s');
        switch ($MassageCoupon->valid_type) {
            case CouponValidType::TIME['value']:
                list($start_time, $end_time) = $MassageCoupon->valid_time;
                if ($start_time && $end_time) {
                    if ($end_time < $nowDate) {
                        throw new \Exception('优惠券已过期');
                    }
                    $MassageCouponCode->start_time = $start_time;
                    $MassageCouponCode->end_time = $end_time;
                } else if ($start_time) {
                    $MassageCouponCode->start_time = $start_time;
                } else if ($end_time) {
                    $MassageCouponCode->end_time = $end_time;
                }
                break;
            case CouponValidType::DAY['value']:
                $MassageCouponCode->start_time = $nowDate;
                $MassageCouponCode->end_time = date('Y-m-d H:i:s', time() + $MassageCoupon->valid_day * 24 * 3600);
                break;
        }
        $MassageCouponCode->save();
    }
    /**
     * 获取优惠券码
     */
    public static function getCouponCode($coupon_id, $num = 1)
    {
        // coupon_id转16进制
        $coupon_id = dechex((int)$coupon_id);
        // coupon_id长度为6位，不足6位前面补0
        $coupon_id = str_pad($coupon_id, 6, '0', STR_PAD_LEFT);
        $createDate = date('YmdHis');
        $round = get_random();
        // 生成优惠券码
        $code = "A{$coupon_id}-{$createDate}-{$round}";
        $couponCode = self::where(['code' => $code])->find();
        if ($couponCode) {
            $num++;
            if ($num > 10) {
                throw new \Exception('优惠券码生成失败');
            }
            return self::getCouponCode($coupon_id, $num);
        }
        return $code;
    }
}
